package cn.tiancaifan.micro.utils;


import cn.tiancaifan.micro.entity.SystemResults;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStreamReader;
import java.sql.SQLException;

@Component
public class DataBaseUtils {
    @Autowired
    DruidDataSource druidDataSource;

    public SystemResults runSqlJs(InputStreamReader reader){
        try {
            ScriptRunner runner=new ScriptRunner(druidDataSource.getConnection());
            runner.runScript(reader);
            SystemResults s = SystemResults.ok("成功");
            s.setCode(0);
            return s;
        } catch (SQLException e) {
            e.printStackTrace();
            SystemResults s = SystemResults.error("失败");
            s.setCode(1);
            return s;
        }
    }
}
